package com.happy_hao.topbiz.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.happy_hao.topbiz.api.MsgApi;
import com.happy_hao.topbiz.common.Result;
import com.happy_hao.topbiz.dto.msgDto.EmailRequest;
import com.happy_hao.topbiz.service.EmailCodeService;
import com.happy_hao.topbiz.util.MailUtil;
import com.happy_hao.topbiz.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class EmailCodeServiceImpl implements EmailCodeService {

    @Resource
    private MsgApi msgApi;

    @Resource
    private MailUtil mailUtil;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Result getEmailCode(String email) {
        String code = RandomUtil.randomNumbers(6);
        EmailRequest emailRequest = new EmailRequest();
        emailRequest.setUsername("2072887718@qq.com");
        emailRequest.setFromName("微服务");
        emailRequest.setToMailbox(email);
        emailRequest.setSubject("邮箱验证码");
        emailRequest.setContent(mailUtil.createRegisterCodeContext(code));
        emailRequest.setStartTime(new Date());
        emailRequest.setEndTime(new Date());
        redisUtil.set(email, code, 60L);
        return msgApi.instantSend(emailRequest);
    }


}
